package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LQuestionsType;
import com.learn.core.dto.LQuestionsTypeDTO;
import com.learn.core.service.ILQuestionsTypeService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LQuestionsTypeController
 * @date 2021.03.22 09:19
 * @describe 题型管理
 */
@Api("题型管理")
@RestController
@RequestMapping("/manager/qtyp")
public class LQuestionsTypeController extends WebController<LQuestionsType> {

    @Autowired
    private ILQuestionsTypeService ilQuestionsTypeService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken()
    @PostMapping("/list")
    public PageResult<LQuestionsType> list(@RequestBody LQuestionsTypeDTO lQuestionsTypeDTO){
        try {
            if (lQuestionsTypeDTO==null||lQuestionsTypeDTO.getPage()==null||lQuestionsTypeDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lQuestionsTypeDTO.getPage(),lQuestionsTypeDTO.getRows());
            QueryWrapper<LQuestionsType> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lQuestionsTypeDTO.getQuestionsTypeName()))
                queryWrapper.like("questions_type_name",lQuestionsTypeDTO.getQuestionsTypeName());
            Page<LQuestionsType> lQuestionsTypes = (Page<LQuestionsType>)ilQuestionsTypeService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lQuestionsTypes)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lQuestionsTypes.getTotal(), lQuestionsTypes, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新题型", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LQuestionsType lQuestionsType, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lQuestionsType!=null){
            lQuestionsType.setUpdateTime(new Date());
            lQuestionsType.setUpdateUser(userId);
            if(ilQuestionsTypeService.updateById(lQuestionsType)){
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加题型", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LQuestionsType lQuestionsType, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lQuestionsType!=null) {
                if (StringUtils.isEmpty(lQuestionsType.getQuestionsTypeName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_QUESTIONS_TYPE_NAME);
                }
                lQuestionsType.setCreateUser(userId);
                lQuestionsType.setCreateTime(new Date());
                if (ilQuestionsTypeService.save(lQuestionsType)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除题型", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LQuestionsType lQuestionsType){
        try {
            if(lQuestionsType!=null){
                if(lQuestionsType.getQuestionsTypeId()!=null){
                    QueryWrapper<LQuestionsType> queryWrapper = new QueryWrapper<LQuestionsType>();
                    queryWrapper.in(true, "questions_type_id", lQuestionsType.getQuestionsTypeId());
                    if(ilQuestionsTypeService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
